package logic

import (
	"context"
	"encoding/json"
	"fmt"
	"time"
	"video/model/cache"
	"video/model/mysql"

	"video/videorpc/internal/svc"
	"video/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type VideoShowLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewVideoShowLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VideoShowLogic {
	return &VideoShowLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 视频详情
func (l *VideoShowLogic) VideoShow(in *videorpc.VideoShowRequest) (*videorpc.VideoShowResponse, error) {
	// todo: add your logic here and delete this line

	key := fmt.Sprintf("userId%d", in.UserId)

	result, _ := cache.Rdb.Get(l.ctx, key).Result()
	var show []*videorpc.VideoShow

	if result == "" {

		show = mysql.VideoShow(mysql.DB, in)

		marshal, _ := json.Marshal(show)

		cache.Rdb.Set(l.ctx, key, marshal, time.Minute*10)

	} else {
		json.Unmarshal([]byte(result), &show)
	}

	return &videorpc.VideoShowResponse{
		Show: show,
	}, nil
}
